package com.dz.config;


import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
//    拦截
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getdefaultWebSecurityManager")  DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
//        设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
//        添加shiro的内置过滤器
        /*
        anon:无需认证就可以访问
        authc:必须认证才能访问
        user: 必须拥有，记住我 功能才能用
        role: 拥有某个角色权限才能访问

         */
        Map<String,String> filteMap=new LinkedHashMap<>();
//        filteMap.put("/login","anno");
//        filteMap.put("/","anno");
        //拦截的页面/
        filteMap.put("/index","authc");
        bean.setFilterChainDefinitionMap(filteMap);
//        设置登录页面
        bean.setLoginUrl("/");

        return bean;
    }


    @Bean(name ="getdefaultWebSecurityManager" )
    public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("getUserRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager=new DefaultWebSecurityManager();

        defaultWebSecurityManager.setRealm(userRealm);

        return defaultWebSecurityManager;
    }


    @Bean(name = "getUserRealm")
    public UserRealm getUserRealm(){
        return new UserRealm();

    }

}
